# food.py
import pygame
import random
from constants import WIDTH, HEIGHT, BLOCK_SIZE, RED, screen, food_sprites


class Food:
    def __init__(self, snake_body=None):
        self.position = None
        if snake_body:
            self.generate(snake_body)
        else:
            self.position = (0, 0)

    def generate(self, snake_body):
        """生成食物，确保不与蛇身重叠"""
        while True:
            food_x = random.randint(0, (WIDTH - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
            food_y = random.randint(0, (HEIGHT - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
            if (food_x, food_y) not in snake_body:
                self.position = (food_x, food_y)
                break

    def draw(self):
        """绘制食物"""
        print(food_sprites)
        print(f"绘制食物在位置: {self.position}")
        # pygame.draw.rect(screen, RED, pygame.Rect(self.position[0], self.position[1], BLOCK_SIZE, BLOCK_SIZE))
        screen.blit(food_sprites["apple"], self.position)
